#include "musicsilder.h"
#include "ui_musicsilder.h"

#include<QMouseEvent>
#include<QDebug>

MusicSilder::MusicSilder(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSilder),
    currentPos(0)
{

    ui->setupUi(this);
    maxWidth=width();
   moveSilder();

}

MusicSilder::~MusicSilder()
{
    delete ui;
}

void MusicSilder::moveSilder()
{
    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),currentPos,ui->outLine->height());
}

void MusicSilder::moveStep(float bf)
{
    //计算currentpos的位置,当时间改变时
    currentPos=bf*maxWidth;
    moveSilder();
}

void MusicSilder::mousePressEvent(QMouseEvent *event)
{
    currentPos=event->pos().x();
    moveSilder();
}

void MusicSilder::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    moveSilder();
    emit setMusicPos(currentPos/(float)maxWidth);
}

void MusicSilder::mouseMoveEvent(QMouseEvent *event)
{
    //必须是在窗口内进行滑动
    QRect rect(0,0,this->width(),this->height());
   if(!rect.contains(event->pos()))
       return ;

    //必须是鼠标按住左键之后在继续滑动

    if(event->buttons()==Qt::LeftButton)
    {
         currentPos=event->pos().x();
          moveSilder();
          emit setMusicPos(currentPos/(float)maxWidth);
    }
}
